package cn.wolfcode.web.controller;

import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.Menu;
import cn.wolfcode.qo.ConsumptionReportQueryObject;
import cn.wolfcode.qo.MenuQueryObject;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IMenuService;
import cn.wolfcode.utils.RequestPermission;
import cn.wolfcode.utils.UserContext;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IMenuService menuService ;


    @RequestMapping("/list")
    @RequestPermission(name="菜单查询",expression = "menu:list")
    public String list(Model model , @ModelAttribute("qo") MenuQueryObject qo){

        PageInfo<Menu> pageInfo = menuService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "systemMenu/list" ;
    }


    @RequestMapping("/delete")
    @RequestPermission(name="菜单删除",expression = "menu:delete")
    public String delete(Long id){
        if (id != null) {
            menuService.delete(id);
        }
        return "redirect:/systemMenu/list" ;
    }


    @RequestMapping("/input")
    public String input(Long id,Model model){
        if (id != null) {
            model.addAttribute("menu",menuService.selectById(id));
        }
        return "/systemMenu/input" ;
    }

    @RequestMapping("/saveOrUpdate")
    @RequestPermission(name="菜单编辑",expression = "menu:save")
    public String save(Menu menu , Long id){
        if (id != null){
            menuService.update(menu);
            return "redirect:/systemMenu/list";
        }
        menuService.insert(menu);
        return "redirect:/systemMenu/list";
    }

    @RequestMapping("/changeState")
    @ResponseBody
    public JsonResult changeState(int state,Long id){
        menuService.changeState(state ,id);
        if (state==1){
            return new JsonResult(true,"启用成功");
        }else{
            return new JsonResult(true,"禁用成功");
        }
    }


}
